package com.vision.sales.check.cl.ui;

import static com.vision.sales.cl.SalesServices.checkSvc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.ParseErrorEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.info.Info;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.UI;
import com.vision.sales.cl.event.CheckCreatedEvent;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cm.db.data.Check;

/**
 * 
 * @author Mark
 *
 */
public class CreateCheckWindow implements IsWidget, Editor<Check> {
	
	interface Binder extends UiBinder<Widget, CreateCheckWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);

	@UiField Window window;
	@UiField TextField bank;
	@UiField TextField checkNo;
	@UiField DateField checkDate;
	@UiField NumberField<Double> amount;
	@UiField DateField dateDeposited;
	@UiField DateField dateCleared;
	@UiField TextArea remarks;
	
	// Ignored by the form driver since these are not fields in the Check
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;
	
	@UiField(provided = true)
	NumberPropertyEditor<Double> doublePropertyEditor = new DoublePropertyEditor();
	
	@UiField(provided = true)
	NumberFormat numberFormat = NumberFormat.getFormat("0.00");
	
	interface CheckDriver extends SimpleBeanEditorDriver<Check, CreateCheckWindow> { }
	
	private CheckDriver driver = GWT.create(CheckDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(SalesResources.INSTANCE.checks());
		
		driver.initialize(this);
		driver.edit(new Check());
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	@UiHandler("checkDate")
	public void onCheckDateParseError(ParseErrorEvent event) {
		Info.display("Parse Error", event.getErrorValue() + " could not be parsed as a date");
	}
	
	@UiHandler("dateDeposited")
	public void onDateDepositedParseError(ParseErrorEvent event) {
		Info.display("Parse Error", event.getErrorValue() + " could not be parsed as a date");
	}
	
	@UiHandler("dateCleared")
	public void onDateClearedParseError(ParseErrorEvent event) {
		Info.display("Parse Error", event.getErrorValue() + " could not be parsed as a date");
	}

	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		Check check = driver.flush();
		if (driver.hasErrors())
			return;
		
		UI.mask(window, "Creating check...");
		checkSvc().create(check, new Callback<Check>(window) {
			@Override
			protected void handleSuccess(Check result) {
				window.hide(saveBtn);
				
				BaseModule.fireEvent(new CheckCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}

}
